﻿namespace Leetcode.N0002;

public class Solution
{
    public ListNode AddTwoNumbers(ListNode l1, ListNode l2)
    {
        var sum = l1.val + l2.val;

        return new ListNode(sum % 10, this.AddTwoNumbers(l1.next, l2.next, sum >= 10 ? true : false));
    }

    private ListNode AddTwoNumbers(ListNode l1, ListNode l2, bool value)
    {
        if (l1 == null && l2 == null)
        {
            if (value)
            {
                return new ListNode(1);
            }
            else
            {
                return null;
            }
        }


        var sum = (l1 == null ? 0 : l1.val) + (l2 == null ? 0 : l2.val) + (value ? 1 : 0);


        return new ListNode(sum % 10, this.AddTwoNumbers(l1 == null ? null : l1.next, l2 == null ? null : l2.next, sum >= 10 ? true : false));

    }
}
